class ApiParser:
    def __init__(self, filename, oes_func_prefix):
        self.filename = filename
        self.oes_func_prefix = oes_func_prefix

        self.comment_params = None
        self.state = None
        self.last_line = None

        self.func_type = None
        self.func_name = None
        self.func_param_str = None
        self.funcs = dict()

    def process_line(self, line):
        if self.state is None and line.startswith('/*'):
            self.comment_params = dict()
            self.state = 'func_comment'
            return

        if self.state == 'func_comment' and '@param' in line:
            tokens = line.split('@param')[-1].split()
            ptype = 'in'
            pname = tokens[0]
            if tokens[0] == '[out]':
                ptype = 'out'
                pname = tokens[1]
            if tokens[0] == '[in]':
                pname = tokens[1]
            self.comment_params[pname] = ptype
            return

        if self.state == 'func_comment' and line.endswith('*/'):
            self.state = None
            # self.comment_params = None
            return

        if self.state is None and self.oes_func_prefix in line:
            self.state = 'func_define'

            if not ' ' in line.split('(')[0]:
                self.func_type = self.last_line
            else:
                self.func_type = line.split('(')[0].rsplit(' ', 1)[0].strip()
            self.func_type = self.func_type.replace('static', '').replace(
                '__inline', '').strip()
            self.func_name = line.split('(')[0].rsplit(' ', 1)[-1].strip()
            self.func_param_str = ''
            if line.endswith('();'):
                self.funcs[self.func_name] = {
                    'ret_type': self.func_type,
                    'params': []
                }
                self.state = None
                return
            if not line.endswith('('):
                self.func_param_str += line.split('(')[-1]
            return

        if self.state == 'func_define':
            if line.endswith(');'):
                self.func_param_str += line[:-2]
                params = []
                tokens = self.func_param_str.split(',')
                for token in tokens:
                    ptype, pname = token.rsplit(' ', 1)
                    ptype = ptype.strip()
                    pname = pname.strip()
                    if pname[0] == '*':
                        pname = pname[1:]
                        ptype = ptype + '*'
                    if pname.endswith('[]'):
                        pname = pname[:-2]
                        ptype = ptype + '[]'
                    params.append({
                        'name':
                        pname,
                        'type':
                        ptype,
                        'inout':
                        self.comment_params.get(pname, 'None')
                    })
                self.funcs[self.func_name] = {
                    'ret_type': self.func_type,
                    'params': params
                }
                self.state = None
            else:
                self.func_param_str += line
            return

    def parse(self):
        with open(self.filename, 'r') as fin:
            for line in fin:
                line = line.strip()
                self.process_line(line)
                self.last_line = line
        return self.funcs
